<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_AJAREKS.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
Class_PWD::valid_auth(AOKI_AUTH_AJAREKS,$login['staff_auth']);

$dao  = new Class_M_AJAREKS ;
//$zone = Class_M_OTHER::get_zone_array();
//print_r($aoki_area_zone);
if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$datas  = $dao->getByUseid( $_GET['id'] );
		//データ整形
		$forms["id"] = $_GET['id'];
		$firstelement = TRUE;
		foreach ($datas as $k=>$v) {
			if ($firstelement) {
				$firstelement         = FALSE;
				$forms["userid"]      = $v["id"];
				$forms["user_name"]   = $v["user_name"];
				//$forms["zone_name"]   = $zone[$v["zone_cd"]];
				$forms["m_user_name"] = $v["m_user_name"];
				$forms["u_time"]      = $v["u_time"];
				$forms["area_zone"]   = $aoki_area_zone[$v["auth"]];
				$forms["auth"]        = $v["auth"];
			}
			if ( !empty($v["shop_cd"]) )
			$forms["mutirow"] []=array("shop_cd"=> $v["shop_cd"], "shop_name" => $v["shop_name"]);
		}
		
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		
	}else{
		//$forms['auth'] = 0;
	}

}else{
	$forms = $_POST;
	
	if (isset($forms["shop_cd"])) {
		foreach ($forms["shop_cd"] as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v, "shop_name" => $forms["shop_name"][$k]);
		}
	} 
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		
		
		if ( $forms["auth"] == '3' ) {
			//$err_mes["zone_cd"]  = $err->check( $forms["zone_name"], array("EXIST") );
			$shop_users =  $dao->getUsersByAja(3);
		} else {
			$shop_users =  $dao->getUsersByAja(2);
		}
/*		
		if( $err_mes['shop_cd'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" shop_cd = '%s'", $forms['shop_cd'] );
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['shop_cd'] = '<p class="error">この店舗は重複しています</p>';
				}
			}else{
				//$where = sprintf(" select  shop_cd = '%s'", $forms['shop_cd'] );
			}
		}
		*/
		foreach ($forms["mutirow"] as $k=>$v) {			
			$shop_cd = $v["shop_cd"] ;
			if (isset($shop_users[$shop_cd])) {
				foreach($shop_users[$shop_cd]   as $kk => $vv ) {
					if ($vv  != $forms["userid"]) {
						$err->clear = false;
						$shop_name = $v["shop_name"] ;
						$err_mes['shop_cd'] =$err_mes['shop_cd'].'<p class="error">店舗['.$shop_name.']は重複しています</p>';
						break;
					}
				}
			}
			
		}
		//if ( $err->clear ){
			//$cols     = $dao->get_cols();
			
			//foreach($cols as $kcol => $val) {
			//	$values[$kcol] = $forms[$kcol] ;
			//}
			//桁数チェック
			//$maxlens = $dao->get_maxlens();
			//foreach ( $maxlens as $k => $v ) {
			//	$err_mes[$k] =$err->check_size($values[$k], $v);
			//}
		//}
		
		if( $err->clear ){
			try {
				$dao->begin_trans();
				$executeflg = TRUE;
				/*
				$zone_cd = 0;
				if  ($forms["auth"] == '3') {
					$zone_cd = class_M_OTHER::do_zone($forms["zone_name"]);
				}
				*/
				
				if( $forms['id'] == '' ){
					foreach ($forms["mutirow"] as $k=>$v) {
						$values = array();
						$values["userid"]         =  $forms["userid"]; 
						$values["user_name"]      =  $forms["user_name"];
						//$values["zone_cd"]        =  $zone_cd;
						$values["shop_cd"]        =  $v["shop_cd"];
						
						//$values["shop_name"]      =  $v["shop_name"];
						//$values["disp_ng"]        =  0;
						if ($dao->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					if  ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '登録が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '登録が失敗しました。');
					}
				}else{
					/*更新か削除かチェック整形*/
					$dataadd  = array();
					$datadel  = array();
					$dataedit = array();
					
					/*
					$disp_ng = $forms["disp_ng"]?$forms["disp_ng"]:0;
					*/
					$dbarray = $dao->getByUseid( $forms["userid"] );
					//print_r($dbarray);
					//削除.更新のデータ作成
					foreach($dbarray as $k=>$v) {
						$delflg = TRUE;
						foreach($forms["mutirow"] as $kk=>$vv) {
							if ($v["shop_cd"] == $vv["shop_cd"] ) {
								$delflg = FALSE;
								break;
							}
						}
						if ($delflg){
							$datadel[]  = $v["cid"];
						} else {
							$dataedit[] = $v["cid"];
						}
					}
					
					//追加のデータ作成
					foreach($forms["mutirow"] as $k=>$v) {
						$taddflg = TRUE;
						foreach($dbarray as $kk=>$vv) {
							if ($v["shop_cd"] == $vv["shop_cd"] ) {
								$taddflg = FALSE;
								break;
							}
						}
						if ($taddflg){
							$dataadd[] = $v["shop_cd"];
						}
					}
					//データ削除
					foreach ($datadel as $k=>$v) {
						if ($dao->remove($v)) {
						} else {
							$executeflg = FALSE;
						}
					}
					//データ修正
					foreach ($dataedit as $k=>$v) {
						$values = array();
						//$values["userid"]   =  $forms["userid"];
						//$values["zone_cd"]  =  $zone_cd;
						//$values["disp_ng"]          =  $disp_ng;
						if ($dao->edit( $v ,$values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					//データ追加
					foreach ($dataadd as $k=>$v) {
						$values = array();
						$values["userid"]         =  $forms["userid"]; 
						$values["user_name"]      =  $forms["user_name"];
						$values["shop_cd"]        =  $v;
						//$values["zone_cd"]        =  $forms["zone_cd"];
						//$values["disp_ng"]          =  $disp_ng;
						
						if ($dao->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					if  ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '更新が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '更新が失敗しました。');
					}
				}
			
			} catch (Exception $e) {
				$dao->rollback_trans();
				$smarty->assign('message', 'ＤＢエラーで失敗しました。');
			}
			
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

//$smarty->assign('areazone', $aoki_area_zone );


if( isset( $forms ) )   $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

/*
$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}
$smarty->assign('$isG4', $isG4 );

if ($phase != 'complete') {
	$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);
	$smarty->assign('block', $block );
	
	$local= Class_M_OTHER::get_local_array(PLEASE_SELECT_ARR);
	$smarty->assign('local', $local );
}
*/

//$smarty->assign('zone',  $zone );
$smarty->assign('phase', $phase );

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->debugging = true;
$smarty->display('majareks_form.html');
?>